

$(function(){
	var nowDate = new Date();
	$("#beginDate").html(getYesterday());
	$("#endDate").html(getYesterday());
	//点击查询按钮
	var isFirst = true;
	document.getElementById("selector").addEventListener('tap',function(){
		this.href = '#selectorPage';
		if(isFirst){
			isFirst = false;
			createSubjectStockTable('',1,'','','');
		}
	});
//-----------------------------查询   一级界面---------------------------------------------------
	function createSubjectStockTable(category,outOrInside,isEmployee,subject,stock){
		var subjectStockTable = document.getElementById('subjectStockTable');
		//描绘之前先清空
		while(subjectStockTable.hasChildNodes()){
			subjectStockTable.removeChild(subjectStockTable.firstChild);
		}
		var thead;//表头
		var tr;
		var th;
		var tbody;//表格内容
		var td;
		var div;
		var input;
		//创建fragment   只操作页面一次   减少dom的更新
		var fragment = document.createDocumentFragment();

		thead = document.createElement('thead');
		tr = document.createElement('tr');
		th = document.createElement('th');
		th.style = 'height: 40px;';
		span = document.createElement('span');
		span.className = 'mui-spinner';
		th.appendChild(span);
		tr.appendChild(th);
		thead.appendChild(tr)
		fragment.appendChild(thead);
		subjectStockTable.appendChild(fragment);


		var subjectStockArray = new Array();
		var isSucceed = false;
		subjectWareGet(function(isSuccess,data){
			isSucceed = isSuccess;
			if(isSuccess == false){
				mui.alert(data);
			} else{
				for(var i = 0; i < data.length; i++){
					subjectStockArray.push(data[i]);
				}
			}
			//描绘之前先清空
			while(subjectStockTable.hasChildNodes()){
				subjectStockTable.removeChild(subjectStockTable.firstChild);
			}

			if(!isSucceed){
				thead = document.createElement('thead');
				tr = document.createElement('tr');
				th = document.createElement('th');
				th.style = 'height: 40px;';
				th.innerHTML = '暂无数据!'
				tr.appendChild(th);
				thead.appendChild(tr)
				fragment.appendChild(thead);
				subjectStockTable.appendChild(fragment);
				return;
			}
//		subjectStockArray = [{"subjectId":"马自英","subjectNW":0,"subjectNick":"18020770786","subjectTypeId":"S001","subjectTypeName":"会员","subjectYG":0,"wareId":"马自英X00","wareName":"自有虚拟仓"},{"subjectId":"P00504","subjectNW":0,"subjectNick":"360礼品网","subjectTypeId":"S003","subjectTypeName":"供应商","subjectYG":0,"wareId":"P00504X00","wareName":"自有虚拟仓"},{"subjectId":"马自英","subjectNW":0,"subjectNick":"18020770786","subjectTypeId":"S001","subjectTypeName":"会员","subjectYG":0,"wareId":"马自英X00","wareName":"自有虚拟仓"},{"subjectId":"P00504","subjectNW":0,"subjectNick":"360礼品网","subjectTypeId":"S003","subjectTypeName":"供应商","subjectYG":0,"wareId":"P00504X00","wareName":"自有虚拟仓"},{"subjectId":"马自英","subjectNW":0,"subjectNick":"18020770786","subjectTypeId":"S001","subjectTypeName":"会员","subjectYG":0,"wareId":"马自英X00","wareName":"自有虚拟仓"},{"subjectId":"P00504","subjectNW":0,"subjectNick":"360礼品网","subjectTypeId":"S003","subjectTypeName":"供应商","subjectYG":0,"wareId":"P00504X00","wareName":"自有虚拟仓"}]

			//创建table表头
			thead = document.createElement('thead');
			thead.className = 'rowHeader';
			tr = document.createElement('tr');
			var thArray = ["主体","单元"];
			for (var i = 0; i < thArray.length + 1; i++) {
				th = document.createElement('th');
				th.style = (i == 0 ? 'width:10%;' : '');
				if(i == 0){
					div = document.createElement('div');
					div.className = 'mui-checkbox';
					div.style = 'height:100%;width:100%'
					input = document.createElement('input');
					input.className = 'checkAll';
					input.type = 'checkbox';
					input.style = 'position:static;';
					div.appendChild(input);
					th.appendChild(div);
				} else {
					th.innerHTML = thArray[i - 1];
				}
				tr.appendChild(th);
			}
			thead.appendChild(tr)
			fragment.appendChild(thead);
			//创建table的内容
			tbody = document.createElement('tbody');
			var tdArray = ["subjectNick","wareName"];
			for (var i = 0; i < subjectStockArray.length; i ++) {
				tr = document.createElement('tr');
				tr.className = (i % 2 === 0 ? 'info' : '');
				for (var j = 0; j < tdArray.length + 1; j++) {
					td = document.createElement('td');
					if(j == 0){
						div = document.createElement('div');
						div.className = 'mui-checkbox';
						div.style = 'height:100%;width:100%'
						input = document.createElement('input');
						input.className = 'checkOne';
						input.type = 'checkbox';
						input.style = 'position:static;';
						div.appendChild(input);
						td.appendChild(div);
					} else {
						if(j == 2){
							td.className = 'ware';
							td.setAttribute('data-wareid',subjectStockArray[i].wareId);
							td.setAttribute('data-subjectId',subjectStockArray[i].subjectId);
						}
						// td.innerHTML = (j < tdArray.length - 1 ? (subjectStockArray[i][tdArray[j - 1]]) : (j === tdArray.length - 1 ? (subjectStockArray[i][tdArray[j - 1]] == '0' ? '内部主体' : '外部主体') : (subjectStockArray[i][tdArray[j - 1]] == '0' ? '否' : '是')));
						td.innerHTML = subjectStockArray[i][tdArray[j - 1]]
					}
					tr.appendChild(td);
				}
				tbody.appendChild(tr);
			}
			fragment.appendChild(tbody);
			//将fragment绘画出来
			subjectStockTable.appendChild(fragment);

			//全选
			var selectorStockStr = "";//选择时将选中的结果存放到selectorSubjectStockShow
			var selectorStockIDs = "";
			var selectorStockIDs2 = "";
			$(".checkAll").click(function(){
				selectorStockStr = "";
				selectorStockIDs = "";
				selectorStockIDs2 = "";
				var isClickAll = this.checked;
				$(".checkOne").each(function(){
					this.checked = isClickAll;
					if(this.checked == true){
						selectorStockStr += ($(this).parents('td').siblings('.ware').html() + ',');
						selectorStockIDs += ($(this).parents('td').siblings('.ware').attr('data-wareid') + ',');
						selectorStockIDs2 += ($(this).parents('td').siblings('.ware').attr('data-subjectId') + ',');
					}
				});
				$("#selectorSubjectStockShow").html(selectorStockStr.substring(0,selectorStockStr.length - 1));
				$("#selectorSubjectStockShow").attr('data-wareids',selectorStockIDs.substring(0,selectorStockIDs.length - 1));
				$("#selectorSubjectStockShow").attr('data-subjectId',selectorStockIDs2.substring(0,selectorStockIDs2.length - 1));

			});
			//单选
			$(".checkOne").click(function(){
				$(".checkAll").prop("checked",($(".checkOne").length === $(".checkOne:checked").length));
				selectorStockStr = "";
				selectorStockIDs = "";
				selectorStockIDs2 = "";
				$(".checkOne:checked").each(function(){
					selectorStockStr += ($(this).parents('td').siblings('.ware').html() + ',');
					selectorStockIDs += ($(this).parents('td').siblings('.ware').attr('data-wareid') + ',');
					selectorStockIDs2 += ($(this).parents('td').siblings('.ware').attr('data-subjectId') + ',');
				});
				$("#selectorSubjectStockShow").html(selectorStockStr.substring(0,selectorStockStr.length - 1));
				$("#selectorSubjectStockShow").attr('data-wareids',selectorStockIDs.substring(0,selectorStockIDs.length - 1));
				$("#selectorSubjectStockShow").attr('data-subjectId',selectorStockIDs2.substring(0,selectorStockIDs2.length - 1));
			});
		},category,outOrInside,isEmployee,subject,stock);


	}

	//获取主体单元
	function subjectWareGet(callBack,category,outOrInside,isEmployee,subject,stock){
		callBack = callBack||mui.noop;
		Utils.request( Utils.Api.rptSubjectWareGet, {
			qrySubjectType : category,
			qrySubjectNW   : outOrInside,
			qrySubjectYG   : isEmployee,
			qrySubjectName : subject,
			qryWareName    : stock,
			start: 0,
			limit: 10,
			pageIndex: 0
		}, function(data){
			if(data.rows.length > 0){
				callBack(true,data.rows);
			} else {
				callBack(false,'暂无数据!');
			}
		})
	}

//查询一级界面确定按钮
	$("#confirmBtn").click(function(){
		var beginDate = $('#selectorBeginDateShow').html();
		if(isNull(beginDate)){
			mui.alert("请选择开始日期!")
			return;
		}
		var endDate = $('#selectorEndDateShow').html();
		if(isNull(endDate)){
			mui.alert("请选择结束日期!")
			return;
		}
		var wareIDs = $('#selectorSubjectStockShow').attr('data-wareids');
		var subjectId = $('#selectorSubjectStockShow').attr('data-subjectId');
		if(isNull(wareIDs)){
			mui.alert("请选择单元!")
			return;
		}
		var subjectStocks = "";
		$(".checkOne:checked").each(function(){
			subjectStocks += ($(this).parents('td').siblings().first().html() + '(' + $(this).parents('td').siblings('.ware').html() + '),');
		});

		mui.back();
		createmainTable(beginDate,endDate,wareIDs,subjectStocks.substring(0,subjectStocks.length - 1),subjectId);
	});
//-----------------------------查询   二级界面---------------------------------------------------
//获取分类
	var subjectTypeArray = new Array();
	subjectTypeArray.push({text: '全部',value: ''});
//获取分类选择器的数据来源
	Utils.request(Utils.Api.rptSubjectWareGet, {}, function(data){
		for(var i = 0; i < data.length; i++){
			subjectTypeArray.push(data[i]);
		}
	});

//为开始日期和结束日期设置默认值
	$("#selectorBeginDateShow").html(getYesterday());
	$("#selectorEndDateShow").html(getYesterday());
	$("#selectorCategoryShow").html(subjectTypeArray[0].text).attr('data-value',subjectTypeArray[0].value);
	$("#selectorOutOrInsideShow").html("内部主体").attr('data-value',1);
	$("#selectorIsEmployeeShow").html("全部").attr('data-value','');
//日期   分类  内外  员工选择器
	$(".myPicker").click(function(){
		var self = this;
		//创建普通的popPicker
		var popPicker = new mui.PopPicker();
		var popDataArray = new Array();
		//创建日期形式的Depicker
		var datePicker = new mui.DtPicker({
			type: "date",
			endDate: nowDate
		});
		switch ($(self).attr('id')){
			//开始日期选择器
			case 'selectorBeginDate':
				datePicker.setSelectedValue(getMonthStartDate());
				datePicker.show(function(items) {
					$("#selectorBeginDateShow").html(items.value);
					datePicker.dispose();
				});
				break;
			//结束日期选择器
			case 'selectorEndDate':
				datePicker.show(function(items) {
					$("#selectorEndDateShow").html(items.value);
					datePicker.dispose();
				});
				break;
			//分类选择器
			case 'selectorCategory':
				popDataArray = [];//将数组清空
				popDataArray = subjectTypeArray;
				popPicker.setData(popDataArray);
				popPicker.show(function(items) {
					$("#selectorCategoryShow").html(items[0].text);
					$("#selectorCategoryShow").attr('data-value',items[0].value);
					popPicker.dispose();
				});
				break;
			//内外选择器
			case 'selectorOutOrInside':
				popDataArray = [];//将数组清空
				popDataArray = [{value: '1' ,text: '内部主体'},{value: '0' ,text: '外部主体'},{value: '' ,text: '全部'}];
				popPicker.setData(popDataArray);
				popPicker.show(function(items) {
					$("#selectorOutOrInsideShow").html(items[0].text);
					$("#selectorOutOrInsideShow").attr('data-value',items[0].value);
					popPicker.dispose();
				});
				break;
			//员工选择器
			case 'selectorIsEmployee':
				popDataArray = [];//将数组清空
				popDataArray = [{value: '' ,text: '全部'},{value: '1' ,text: '是'},{value: '0' ,text: '否'}];
				popPicker.setData(popDataArray);
				popPicker.show(function(items) {
					$("#selectorIsEmployeeShow").html(items[0].text);
					$("#selectorIsEmployeeShow").attr('data-value',items[0].value);
					popPicker.dispose();
				});
				break;
			default:
				break;
		}
	});
//确定按钮
	$("button#selectorBtnOK").click(function(){
		var category = $('#selectorCategoryShow').attr('data-value');
		if(isNull(category)){
			category = subjectTypeArray[0].value;
		}
		var outOrInside = $('#selectorOutOrInsideShow').attr('data-value');
		if(isNull(outOrInside)){
			outOrInside = 1;
		}
		var isEmployee = $('#selectorIsEmployeeShow').attr('data-value');
		if(isNull(isEmployee)){
			isEmployee = '';
		}
		var subject = $("#selectorSubjectShow").val();
		if(isNull(subject)){
			subject = "";
		}
		var stock = $("#selectorStockShow").val();
		if(isNull(stock)){
			stock = "";
		}
		mui.back();
		createSubjectStockTable(category,outOrInside,isEmployee,subject,stock);

	});
})
